package com.sh4.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sh4.service.IfilesService;
import com.sh4.util.ActionEnter;
import com.sh4.util.Base64Utils;
import com.sh4.util.Result;
import com.sh4.util.Sh4Utils;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "upload")
public class UploadController {

	private static String separator = File.separator;

	private static String dirName = "base64images";

	private static Logger log = Logger.getLogger("UploadController");

	@Autowired
	IfilesService ifilesService;

	/***
	 * 保存base64上传的额图片到文件夹
	 * 
	 * @param request
	 */
	@RequestMapping(value = "base64", method = RequestMethod.POST)
	public void uploadBase64(@RequestBody JSONObject params, HttpServletRequest request) {
		// 获取项目所在的webapps的路径
		String path = Sh4Utils.getProjetRootPath();

		path = path + request.getContextPath() + "files" + separator + dirName + separator + Sh4Utils.getDateStr();
		Base64Utils.Base64ToImage(params.getString("base64Str"), path);

//		if (folder.exists()) {
//			log.info("已存在文件夹");
//		} else {
//			log.info("文件夹不存在，创建问价夹");
//			folder.mkdirs();
//		}
	}

	@RequestMapping(value = "uploadFile", method = RequestMethod.POST)
	public Result uploadFile(MultipartFile file, HttpServletRequest request) {

		Result result = new Result(200, "success");

		try {

			log.info("getContentType = " + file.getContentType());
			log.info("getName = " + file.getName());
			log.info("getOriginalFilename = " + file.getOriginalFilename());
			log.info("getBytes = " + file.getBytes() + "");
			log.info("getInputStream = " + file.getInputStream().toString());
			log.info("getSize = " + file.getSize());
			JSONObject res = Sh4Utils.saveImageFile2Disk(file, request);
			// 原文件名称
			res.put("fileId", ifilesService.saveIfiles(res));
			result.setData(res);

		} catch (Exception e) {

			result.setStatus(300);
			result.setMsg("error");
		}

		return result;
	}

	@RequestMapping(value = "/config")
	public void config(HttpServletRequest request, HttpServletResponse response) {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,X_Requested_With");
		response.setHeader("Content-Type" , "text/html");
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		System.out.println("进入UEditorController");

		try {
			String exec = new ActionEnter(request, rootPath).exec();
			PrintWriter writer = response.getWriter();
			writer.write(exec);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
